package controller;

import entity.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import service.StoreService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/store")
public class StoreController {
    @Resource
    private StoreService storeService;

    @RequestMapping("/basic")
    public String basic(@RequestParam(required = false) String find,
                        @RequestParam(required = false) String opt,
                        HttpSession session,
                        Model model){
        model.addAttribute("ss_shop_Store",find);
        model.addAttribute("ss_shop_Type",opt);
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(find);
        map.put("ss_shop_Store",find);
        map.put("ss_shop_Type",opt);
        System.out.println(map);
        List<s_shop> list = storeService.getS_shopAll(map);
        System.out.println("来了老弟");
        System.out.println(list);
        session.setAttribute("s_shopList",list);
        return "store/basic";
    }
    @RequestMapping("/goodsManage")
    public String goodsManage(@RequestParam(required = false) String find,
                              @RequestParam(required = false) String opt,
                              HttpSession session,
                              Model model){
        model.addAttribute("sc_commodity_Name",find);
        model.addAttribute("sc_commodity_Type",opt);
        Map<String,Object> map = new HashMap<String,Object>();
        System.out.println(find);
        map.put("sc_commodity_Name",find);
        map.put("sc_commodity_Type",opt);
        System.out.println(map);
        List<s_commodity> list = storeService.getS_commodityAll(map);
        System.out.println("来了老弟");
        System.out.println(list);
        session.setAttribute("s_commdityList",list);
        return "store/goodsManage";
    }
    @RequestMapping("/tradingSearch")
    public String tradingSearch(@RequestParam(required = false) String find,
                                @RequestParam(required = false) String opt,
                                @RequestParam(required = false) String pastTime,
                                @RequestParam(required = false) String futureTime,
                                HttpSession session,
                                Model model){
        model.addAttribute("st_transaction_Name",find);
        model.addAttribute("st_transaction_Type",opt);
        model.addAttribute("pastTime",pastTime);
        model.addAttribute("futureTime",futureTime);
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(pastTime!=null
                &&pastTime!=""){
            try {
                Date pastTime1=sdf.parse(pastTime);
                map.put("pastTime",pastTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("pastTime","");
        }
        if(futureTime!=null
                &&futureTime!=""){
            try {
                Date futureTime1=sdf.parse(futureTime);
                map.put("futureTime",futureTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("futureTime","");
        }
        System.out.println(find);
        map.put("st_transaction_Name",find);
        map.put("st_transaction_Type",opt);
        System.out.println(map);
        List<s_transaction> list = storeService.getS_transactionAll(map);
        System.out.println("来了老弟");
        System.out.println(list);
        session.setAttribute("s_transactionList",list);
        return "store/tradingSearch";
    }
    @RequestMapping("/complaintsSearch")
    public String complaintsSearch(@RequestParam(required = false) String find,
                                   @RequestParam(required = false) String opt,
                                   @RequestParam(required = false) String pastTime,
                                   @RequestParam(required = false) String futureTime,
                                   HttpSession session,
                                   Model model){
        model.addAttribute("sc_complaint_Shopname",find);
        model.addAttribute("sc_complaint_Type",opt);
        model.addAttribute("pastTime",pastTime);
        model.addAttribute("futureTime",futureTime);
        Map<String,Object> map = new HashMap<String,Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if(pastTime!=null
                &&pastTime!=""){
            try {
                Date pastTime1=sdf.parse(pastTime);
                map.put("pastTime",pastTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("pastTime","");
        }
        if(futureTime!=null
                &&futureTime!=""){
            try {
                Date futureTime1=sdf.parse(futureTime);
                map.put("futureTime",futureTime1);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else{
            map.put("futureTime","");
        }
        System.out.println(find);
        map.put("sc_complaint_Shopname",find);
        map.put("sc_complaint_Type",opt);
        System.out.println(map);
        List<s_complaint> list = storeService.getS_complaintAll(map);
        System.out.println(list);
        session.setAttribute("s_complaintList",list);
        return "store/complaintsSearch";
    }
       
    @RequestMapping("/shanchu")
    @ResponseBody
    public Object modityStore(@RequestParam(required = false) String id){
        System.out.println("id-----:"+id);
    int num=  storeService.deleteS_shopById(id);
        System.out.println("num---"+num);
        return "true";
    }
    //查看
    @RequestMapping("/viewHotel")
    public String view(@RequestParam(required = false)String ss_shop_Id,Model model){
        System.out.println(ss_shop_Id);
        s_shop store =storeService.getBasicView(ss_shop_Id);
        model.addAttribute("store",store);
        System.out.println("进来了!!!!");
        System.out.println(store);
        return "store/addStore";
    }
    @RequestMapping("/add")
    public String addHotel(@ModelAttribute s_shop shop){
        return "/store/addStore";
    }

    @RequestMapping("/add.do")
    @ResponseBody
    public Object add1(s_shop shop, HttpServletRequest request,

                       @RequestParam(value="myImage",required = false) MultipartFile path){

        System.out.println("添加信息");
        String filePath = null;
        //用来区分错误提示信息
        String errorInfo=null;
        // 先获取当前请求的全路径
        String p = request.getSession().getServletContext()
                .getRealPath("static" + File.separator + "uploadFile");
        System.out.println(p);
        // 解决用户上传的文件名和现在服务器上保存的
        // 文件名冲突的问题
        // 先获取旧的文件名
        String oldFileName = path.getOriginalFilename();
        System.out.println(oldFileName);
        // 获取旧文件名的后缀
        String prefix = FilenameUtils.getExtension(oldFileName);
        System.out.println(prefix);// png
        // 判断上传的文件大小是否超出规定
        if (path.getSize() > 50000000) {// 是否超过500k
            System.out.println("图片过大");
            return "../store/addStore";
        }
        // 判断后缀是否符合要求
        if (prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("png")
                || prefix.equalsIgnoreCase("gif")) {
            // 统一改名+后缀
            String newFileName =
                    // 当前系统的毫秒值+随机数+后缀
                    System.currentTimeMillis() + RandomUtils.nextInt(100000) + "." + prefix;
            // 使用File对象读取p路径下面的指定文件
            File file = new File(p, newFileName);
            if (!file.exists()) {
                // 创建多级目录
                file.mkdirs();
            }
            // 目录创建成功之后,将上传的文件保存起来
            try {
                path.transferTo(file);
            } catch (IllegalStateException | IOException e) {
                request.setAttribute(errorInfo, "上传失败");
                return "../store/addStore";
                // e.printStackTrace();
            }
            // 给文件字段赋值
            filePath = p + File.separator + newFileName;
            shop.setSs_shop_Picture(filePath);
        }else{
            System.out.println("filePath："+filePath);
            return "../store/addStore";
        }
        int num = storeService.addShop(shop);
        System.out.println(shop);
        System.out.println("num："+num);
        if(storeService.addShop(shop)>0){
            return "true";
        }else {
            return "false";
        }
    }


    @RequestMapping("/modifyStore")
    public String modifyHotel(String ss_shop_Id, Model model,@ModelAttribute s_shop shop){
        System.out.println("id------>>>>>>"+ss_shop_Id);
        s_shop room1=storeService.getRoomById(ss_shop_Id);
        model.addAttribute("store",room1);
        return "store/modifyStore";
    }
    @RequestMapping("/modifyStoreList")
    @ResponseBody
    public Object modifyHotelList(s_shop store,String ss_shop_Createtime1){
        System.out.println("room---->>>>>"+store);
        SimpleDateFormat df =
                new SimpleDateFormat("yyyy-MM-dd");
        try {
           Date date = df.parse(ss_shop_Createtime1);
            store.setSs_shop_Createtime(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (storeService.updateRoom(store)){
            return  "true";
        }else {
            return "false";
        }
    }


}
